package top.italks.manufactureproject.ui.post.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import java.util.ArrayList;
import java.util.List;
import top.italks.manufactureproject.R;
import top.italks.manufactureproject.ui.post.bean.JobPeopleInfo;
import top.italks.manufactureproject.ui.post.bean.UserProductionLineGetAll;
import top.italks.manufactureproject.ui.post.bean.UserProductionLineGetAll.DataBean;

public class PostAdapter extends BaseAdapter {

    private List<UserProductionLineGetAll.DataBean> list;
    private Context mContext;

    @BindView(R.id.num1)
    LinearLayout num1;
    @BindView(R.id.num2)
    LinearLayout num2;
    @BindView(R.id.num3)
    LinearLayout num3;
    @BindView(R.id.num4)
    LinearLayout num4;

    @BindView(R.id.name1)
    TextView name1;
    @BindView(R.id.value1)
    TextView value1;

    @BindView(R.id.name2)
    TextView name2;
    @BindView(R.id.value2)
    TextView value2;
    @BindView(R.id.name3)
    TextView name3;
    @BindView(R.id.value3)
    TextView value3;
    @BindView(R.id.name4)
    TextView name4;
    @BindView(R.id.value4)
    TextView value4;

    public List<DataBean> getList() {
        return list;
    }

    public void setList(
        List<DataBean> list) {
        this.list = list;
    }

    public PostAdapter(Context context) {
        mContext = context;
        list = new ArrayList<>();
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext)
                .inflate(R.layout.post_item_layout, parent, false);
        }
        ButterKnife.bind(this, convertView);

        TextView location = convertView.findViewById(R.id.location);
        location.setText("位置" + list.get(position).getPosition());

        for (JobPeopleInfo info : list.get(position).getList()) {
            switch (info.getStatus()) {
                case 0:
                    name1.setText(String.format("姓名:%s", info.getPeopleName()));
                    value1.setText("体力:" + info.getHp());
                    break;
                case 1:
                    name2.setText(String.format("姓名:%s", info.getPeopleName()));
                    value2.setText("体力:" + info.getHp());
                    break;
                case 2:
                    name3.setText(String.format("姓名:%s", info.getPeopleName()));
                    value3.setText("体力:" + info.getHp());
                    break;
                case 3:
                    name4.setText(String.format("姓名:%s", info.getPeopleName()));
                    value4.setText("体力:" + info.getHp());
                    break;
            }
        }

        num1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.Num1Click(position, name1, value1);
                }
            }
        });
        num2.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.Num2Click(position, name2, value2);
                }
            }
        });
        num3.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.Num3Click(position, name3, value3);
                }
            }
        });
        num4.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.Num4Click(position, name4, value4);
                }
            }
        });

        return convertView;
    }

    private OnItemClickListener listener;

    public OnItemClickListener getListener() {
        return listener;
    }

    public void setListener(
        OnItemClickListener listener) {
        this.listener = listener;
    }

    public interface OnItemClickListener {

        void Num1Click(int position, TextView name, TextView value);

        void Num2Click(int position, TextView name, TextView value);

        void Num3Click(int position, TextView name, TextView value);

        void Num4Click(int position, TextView name, TextView value);
    }
}
